package com.adcc.ags.cgw.managed.proxy;

import com.adcc.ags.cgw.configuration.JobFactory;
import com.adcc.utility.log.Log;
import com.google.common.collect.Lists;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Map;
import java.util.List;

/**
 * 作业代理Managed
 */
public class JobProxy implements Managed{

    // 单例对象
    private static JobProxy instance;

    // 作业计划列表
    private List<Scheduler> schedulerList = Lists.newArrayListWithExpectedSize(2);

    /**
     * 构造函数
     */
    private JobProxy(){

    }

    /**
     * 单例方法
     * @return
     */
    public static JobProxy getInstance(){
        if(instance == null){
            instance = new JobProxy();
        }
        return instance;
    }

    /**
     * 创建Managed
     * @param environment
     */
    public void build(Environment environment,List<JobFactory> jobList) throws Exception{
        try{
            if(environment == null){
                throw new NullPointerException("environment is null");
            }
            if(jobList != null && jobList.size() > 0){
                for(JobFactory factory : jobList){
                    String url = factory.getUrl();
                    Map<String,String> job = factory.getJob();
                    if(!job.containsKey("className") || !job.containsKey("cronExpression")){
                        Log.error("missing className or missing cronExpression");
                        continue;
                    }
                    SchedulerFactory schedulerFactory = new StdSchedulerFactory();
                    Scheduler scheduler = schedulerFactory.getScheduler();
                    Class clazz = Class.forName(job.get("className"));
                    scheduler.getContext().put(clazz.getSimpleName(),url);
                    JobDetail jobDetail = JobBuilder.newJob(clazz)
                            .withIdentity(job.get("className"),"GW")
                            .build();
                    CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(job.get("className"))
                            .withSchedule(CronScheduleBuilder.cronSchedule(job.get("cronExpression")))
                            .build();
                    scheduler.scheduleJob(jobDetail, cronTrigger);
                    schedulerList.add(scheduler);
                }
            }
            environment.lifecycle().manage(this);
        }catch (Exception ex){
            Log.error(JobProxy.class.getName(), "build() error", ex);
            throw ex;
        }
    }

    @Override
    public void start() {
        try{
            for(Scheduler scheduler : schedulerList){
                scheduler.start();
            }
        }catch (Exception ex){
            Log.error(JobProxy.class.getName(),"start() error",ex);
        }
    }

    @Override
    public void stop() {
        try{
            for(Scheduler scheduler : schedulerList){
                scheduler.shutdown();
            }
        }catch (Exception ex){
            Log.error(JobProxy.class.getName(),"stop() error",ex);
        }
    }
}
